/*
 * Created by JFormDesigner on Tue Nov 10 10:19:43 CET 2009
 */

package at.jku.pervasive.gui.jformdesigner;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Map;

import javax.swing.*;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.TitledBorder;
import javax.swing.table.TableColumnModel;

import at.jku.pervasive.Application;
import at.jku.pervasive.ApplicationListener;
import at.jku.pervasive.Position;
import at.jku.pervasive.PositionListener;
import at.jku.pervasive.TagAssignment;
import at.jku.pervasive.gui.editor.ColorEditor;
import at.jku.pervasive.gui.model.AvailableTagsTableModel;
import at.jku.pervasive.gui.renderer.ColorRenderer;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;

/**
 * @author SHOCKIE
 */
public class AvailableTagsPanel extends JPanel implements PositionListener, ApplicationListener
{
	private AvailableTagsTableModel tableModel;

	public AvailableTagsPanel()
	{
		initComponents();

		tableModel = new AvailableTagsTableModel();
		availableTagsTable.setModel(tableModel);

		availableTagsTable.setDefaultRenderer(Color.class, new ColorRenderer(true));
		availableTagsTable.setDefaultEditor(Color.class, new ColorEditor());

		TableColumnModel colModel = availableTagsTable.getColumnModel();
		colModel.getColumn(0).setPreferredWidth(40);
		colModel.getColumn(0).setMinWidth(40);
		colModel.getColumn(0).setMaxWidth(40);

		int colWidth = 25;
		colModel.getColumn(3).setPreferredWidth(colWidth);
		colModel.getColumn(3).setMinWidth(colWidth);
		colModel.getColumn(3).setMaxWidth(colWidth);

		Application.getInstance().addPositionListener(this);
		Application.getInstance().addApplicationListener(this);

		// add filter text field listener
		filterTextField.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyReleased(KeyEvent e)
			{
				String filter = filterTextField.getText();
				if (KeyEvent.VK_ESCAPE == e.getKeyCode())
				{
					// reset filtered text
					filter = "";

					// reset text in the text field
					filterTextField.setText(filter);
				}

				// update table model
				tableModel.setFilter(filter);
			}
		});

		setBorderTitle(0, 0);
	}

	private void initComponents()
	{
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Open Source Project license - unknown
		JScrollPane availableTagsScrollPane = new JScrollPane();
		availableTagsTable = new JTable();
		JPanel propertiesPanel = new JPanel();
		filterTextField = new JTextField();
		CellConstraints cc = new CellConstraints();

		//======== this ========
		setName("this");
		setLayout(new BorderLayout());

		//======== availableTagsScrollPane ========
		{
			availableTagsScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			availableTagsScrollPane.setName("availableTagsScrollPane");

			//---- availableTagsTable ----
			availableTagsTable.setPreferredScrollableViewportSize(new Dimension(300, 50));
			availableTagsTable.setName("availableTagsTable");
			availableTagsScrollPane.setViewportView(availableTagsTable);
		}
		add(availableTagsScrollPane, BorderLayout.CENTER);

		//======== propertiesPanel ========
		{
			propertiesPanel.setName("propertiesPanel");
			propertiesPanel.setLayout(new FormLayout(
				new ColumnSpec[] {
					FormFactory.DEFAULT_COLSPEC,
					FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
					new ColumnSpec(ColumnSpec.LEFT, Sizes.dluX(74), FormSpec.NO_GROW),
					FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
					new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW)
				},
				RowSpec.decodeSpecs("default:grow")));

			//---- filterTextField ----
			filterTextField.setHorizontalAlignment(SwingConstants.RIGHT);
			filterTextField.setName("filterTextField");
			propertiesPanel.add(filterTextField, cc.xywh(5, 1, 1, 1, CellConstraints.FILL, CellConstraints.DEFAULT));
		}
		add(propertiesPanel, BorderLayout.SOUTH);
		// //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	// Generated using JFormDesigner Open Source Project license - unknown
	private JTable availableTagsTable;
	private JTextField filterTextField;
	// JFormDesigner - End of variables declaration //GEN-END:variables

	public void positionReceived(Position newPosition)
	{
		Application app = Application.getInstance();
		boolean added = tableModel.addTag(newPosition.getId());
		if (added)
		{
			int nrOfTags = this.tableModel.getRowCount();
			setBorderTitle(nrOfTags, app.getNumberOfTracingTagIds());

			// app.startedTracingTag(newPosition.getId());
		}

		// update last seen tag
		tableModel.updateLastSeenPosition(newPosition);
	}

	private void setBorderTitle(int nrOfTags, int numberOfTracedTags)
	{
		TitledBorder border = (TitledBorder) getBorder();

		String title;
		if (nrOfTags == 0)
			title = " No tags available - ";
		else if (nrOfTags == 1)
			title = " 1 tag available - ";
		else
			title = " " + nrOfTags + " tags available - ";

		if (numberOfTracedTags == 0)
			title += "tracing no tag";
		else if (numberOfTracedTags > 0)
		{
			title += "tracing " + numberOfTracedTags + " tag";
			if (numberOfTracedTags > 1)
				title += "s";
		}

		title += " ";
		// border.setTitle(title);

		repaint();
	}

	public void startedTracingTag(String tagId)
	{
		setBorderTitle(tableModel.getRowCount(), Application.getInstance().getNumberOfTracingTagIds());
	}

	public void stoppedTracingTag(String tagId)
	{
		setBorderTitle(tableModel.getRowCount(), Application.getInstance().getNumberOfTracingTagIds());
	}

	public void startedListening(String host, int port)
	{
		// reset table model
		tableModel.clearData();
	}

	public void stoppedListening(String host, int port)
	{
		// do nothing
	}

	public void showTagPositions(Map<String, List<Position>> tagPositions)
	{
		// do nothing
	}

	@Override
	public void tagAssignmentChanged(TagAssignment tagPosition, String tagId)
	{
		// TODO Auto-generated method stub

	}
}
